<template>
  <div :class="{'Upload-file-cos':fileList.length>0}">
    <el-upload
      action="#"
      list-type="picture-card"
      :file-list="fileList"
      :http-request="successFn"
      :limit="1"
      :on-remove="removeFn"
    >
      <i class="el-icon-plus" />
    </el-upload>
    <!-- <el-progress v-show="fileList.length>0 && percent!=100" :text-inside="true" :stroke-width="26" :percentage="percent" /> -->
  </div>
</template>

<script>
import COS from 'cos-js-sdk-v5'

// SECRETID 和 SECRETKEY请登录 https://console.cloud.tencent.com/cam/capi 进行查看和管理
var cos = new COS({
  SecretId: 'AKIDW0MbifTA6uZ5QcfYJvgLpHUXO0NXS9cr',
  SecretKey: '83ua5RhmdFg0FJCuOdd0B9wWu4zfEfvy'
})
export default {
  data() {
    return {
      fileList: [
      ],
      percent: 0
    }
  },
  methods: {
    // 删除文件
    removeFn() {
      this.fileList = []
      this.percent = 0
    },
    // 添加文件
    successFn(res) {
      // 由于vue机制，当属性在声明之后去修改，则会报错
      this.fileList.push({ url: '' })
      cos.putObject({
        Bucket: 'fly-hr-project-1312874487', /* 必须 */
        Region: 'ap-nanjing', /* 存储桶所在地域，必须字段 */
        Key: Date.now().toString(), /* 必须 */
        StorageClass: 'STANDARD',
        Body: res.file, // 上传文件对象
        onProgress: (progressData) => {
          // console.log(JSON.stringify(progressData))
          this.percent = progressData.percent * 100
        }
      }, (err, data) => {
        if (!err) {
        //   console.log(err || data)
          this.fileList[0].url = 'http://' + data.Location
          console.log(this.fileList)
        }
      })
    }
  }
}
</script>

<style lang='scss' >
.Upload-file-cos{
  .el-upload--picture-card{
    display: none;
  }
}
</style>
